// pages/mission_info/index.js
var router = require('../../utils/router/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabtitle: ['个人', '企业'],
    page:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * tab 个人与企业
   */
  onChange(event) {
    var that = this
    if (event.detail.index == 0){
       that.setData({
            page:0,
          })
    } else if (event.detail.index == 1){
      that.setData({
        page:1,
      })
    } 
  },

  /**
   * 姓名
   */
  bindnameInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },

  bindcardInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      id_card: e.detail.value
    })
  },

  bindphoneInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      contact: e.detail.value
    })
  },

  bindadsInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      address: e.detail.value
    })
  },

  bindsospersonInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      emergency_name: e.detail.value
    })
  },
  /**
   * 微信
   */
  bindsoswechatInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      emergency_wechat: e.detail.value
    })
  },

  /**
   * 紧急联系人
   */
  bindsosphoneInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      emergency_contact: e.detail.value
    })
  },

  /**
   * 保存
   */
  bind_mission_check_info: function(){
    var that = this
    wx.cloud.callFunction({
      // 云函数名称
      name: 'donate',
      // 传给云函数的参数
      data:{
        "action": "add_personal_donate",
        "name":that.data.name,
        "id_card":that.data.id_card,
        "contact":that.data.contact,
        "address":that.data.address,
        "emergency_name":that.data.emergency_name,
        "emergency_wechat":that.data.emergency_wechat,
        "emergency_contact":that.data.emergency_contact
      }
    })
    .then(res => {
      console.log(res.result._id) // 3
      router.push({ 
        name: 'mission_check_info',
        data: {
          id: res.result._id,
          donate_name:that.data.name
          } 
        })
    })
    .catch(console.error);
  },

  bind_c_name_Input: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      c_name_Input: e.detail.value
    })
  },

  bind_id_card_nput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      id_card_nput: e.detail.value
    })
  },

  bind_c_person_Input: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      c_person_Input: e.detail.value
    })
  },

  bind_c_phone_Input: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      c_phone_Input: e.detail.value
    })
  },


  bind_c_sospersonInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      c_sospersonInput: e.detail.value
    })
  },

  bind_c_soswechatInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      c_soswechatInput: e.detail.value
    })
  },

  bind_c_sosphoneInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      c_sosphoneInpute: e.detail.value
    })
  },

  // bind_c_ads_Input: function(e){
  //   var that = this
  //   console.log(e.detail.value)
  //   this.setData({
  //     c_ads_Input: e.detail.value
  //   })
  // },

  /**
   * 捐赠的企业信息保存
   */
  bind_c_mission_check_info: function(){
    var that = this
    wx.cloud.callFunction({
      // 云函数名称
      name: 'donate',
      // 传给云函数的参数
      data:{
        "action": "add_personal_donate",
        "name":that.data.c_name_Input,
        "id_card":that.data.id_card_nput,
        "contact":that.data.c_person_Input,
        "address":that.data.c_phone_Input,
        "emergency_name":that.data.c_sospersonInput,
        "emergency_wechat":that.data.c_soswechatInput,
        "content": "口罩仅剩2天用量！防护服仅剩3天用量！",
        "emergency_contact":that.data.c_sosphoneInpute
      }
    })
    .then(res => {
      console.log(res.result) 
      router.push({ 
        name: 'mission_check_info',
        data: {
          id: res.result._id,
          donate_name:that.data.c_name_Input
          } 
        })
    })
    .catch(console.error);
  }
})